package com.skillworld.webapp.web.pages.rest.school;

import java.util.List;

import org.apache.tapestry5.annotations.ContentType;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;

import com.skillworld.webapp.model.courseservice.CourseService;
import com.skillworld.webapp.model.lesson.Exam;
import com.skillworld.webapp.model.util.Pagination;
import com.skillworld.webapp.model.util.PermissionDeniedException;
import com.skillworld.webapp.web.util.ErrorMessages;
import com.skillworld.webapp.web.util.Parameter;
import com.skillworld.webapp.web.util.UserSession;

import es.udc.pojo.modelutil.exceptions.InstanceNotFoundException;

@ContentType("text/xml")
public class ListExams 
{
    private static final int DEFAULT_PAGE_NUMBER = 1;
    private static final int DEFAULT_PAGE_SIZE = 20;

	@SuppressWarnings("unused")
    @Property
    private String errorMessage = null;
    
    @SuppressWarnings("unused")
    @Property
    private List<Exam> exams;
        
    @Property
    private Pagination pagination;

    @SuppressWarnings("unused")
    @Property
    private Exam exam; //loop iterator
    
    @Inject
    private CourseService courseService;
    
    @SessionState(create=false)
    private UserSession userSession;
   
    private boolean userSessionExists;
    
    @Inject
    private Request request;

    
    void onPassivate()
    { 
        return; 
    }
    
    void onActivate()
    {        
        if (!userSessionExists)
        {
            errorMessage = ErrorMessages.NOT_LOGGED_IN;
            return;
        }

        String courseParam = request.getParameter("course");
        String lessonsParam = request.getParameter("lessons");
        String pageParam  = request.getParameter("page");
        String psizeParam  = request.getParameter("psize");
        
        try
        { 
            long courseId = Long.parseLong(courseParam); 
            List<Long> lessons = (lessonsParam != null) 
                                    ? Parameter.commaSepIds(lessonsParam) 
                                    : null;
            int page = Parameter.maybeInt(pageParam, DEFAULT_PAGE_NUMBER);
            int pageSize = Parameter.maybeInt(psizeParam, DEFAULT_PAGE_SIZE);
        
            int numberOfExams = 
                    courseService.numberOfExams(userSession.getUserId(), 
                                                    courseId, lessons);
            pagination = new Pagination(numberOfExams, pageSize, page);
            
            if (pageSize <= 0 || !pagination.isValidPage())
            {
                errorMessage = ErrorMessages.INVALID_ARGUMENTS;
                return;
            } 
            
            exams = courseService.listExams(userSession.getUserId(), 
                                                courseId, lessons, 
                                                pagination.getStartIndex(), 
                                                pagination.getCount());
        } 
        catch (InstanceNotFoundException e)
        { errorMessage = ErrorMessages.COURSE_NOT_FOUND; }
        catch (PermissionDeniedException e)
        { errorMessage = ErrorMessages.PERMISSION_DENIED; }
    }
}
